package com.spzx.cart.controller;

import com.spzx.cart.api.domain.CartInfo;
import com.spzx.cart.service.CartInfoService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 快点干早点散
 * @Create 2025/5/8 0:21
 * Description:
 */
@Tag(name = "购物车接口")
@RestController
@RequestMapping
public class CartController extends BaseController {


    @Autowired
    private CartInfoService cartService;

    @Operation(summary = "添加购物车")
    @GetMapping("/addToCart/{skuId}/{num}")
    public AjaxResult addToCart(
            @PathVariable("skuId") Long skuId,
            @PathVariable("num") Integer num) {
        cartService.addToCart(skuId, num);
        return AjaxResult.success();
    }

    @Operation(summary = "查询购物车列表")
    @GetMapping("/cartList")
    public AjaxResult cartList() {
        List<CartInfo> cartList = cartService.cartList();
        return AjaxResult.success(cartList);
    }

    @Operation(summary = "删除购物车商品信息")
    @DeleteMapping("/deleteCart/{skuId}")
    public AjaxResult deleteCart(
            @Parameter(name = "skuId", description = "商品id", required = true)
            @PathVariable("skuId") Long skuId) {
        cartService.deleteCart(skuId);
        return success();
    }

    @Operation(summary = "更新商品状态")
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart(
            @PathVariable(value = "skuId") Long skuId,
            @PathVariable(value = "isChecked") Integer isChecked) {
        cartService.checkCart(skuId, isChecked);

        return success();
    }
    @Operation(summary = "商品全选")
    @GetMapping("allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(@PathVariable Integer isChecked){
            cartService.allCheckCart(isChecked);
        return AjaxResult.success();
    }
    @Operation(summary = "清空购物车")
    @GetMapping("clearCart")
    public  AjaxResult clearCart(){
        cartService.clearCart();
        return success();
    }
    @InnerAuth
    @Operation(summary = "查询当前购物车中勾选商品列表")
    @GetMapping("/getCartCheckedList")
    public R<List<CartInfo>>getCartCheckedList(){
        return R.ok(cartService.getCartCheckedList());
    }
}
